package ch.nortgram.countdown_worldcup_2014;

import java.net.HttpURLConnection;
import java.net.URI;
import java.util.ArrayList;
import java.util.Locale;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import ch.nortgram.countdown_worldcup_2014.R;

public class Old_Vote extends Activity
	{
	int 	vote_id 	 	= 0;
	TypedArray vote_image 	= null;
	String[] vote_desc;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState)
    	{
        super.onCreate(savedInstanceState);
        /*
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.old_dialog_vote);

        vote_image = getResources().obtainTypedArray(R.array.vote_resource);
        
        final ImageView vote_left = (ImageView)findViewById(R.id.img_vote_left);
    	final ImageView vote_right = (ImageView)findViewById(R.id.img_vote_right);
    	final ImageView vote_img = (ImageView)findViewById(R.id.img_vote);
    	
        // spinner-chooser
        final Spinner spinner = (Spinner) findViewById(R.id.spn_chooser);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.vote_name, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new OnItemSelectedListener()
        	{
            public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)
            	{
            	vote_id = arg0.getSelectedItemPosition();
                vote_img.setImageDrawable(vote_image.getDrawable(vote_id));
            	}

            public void onNothingSelected(AdapterView<?> arg0)
	        	{
            	vote_img.setImageDrawable(vote_image.getDrawable(vote_id));
	        	}
        	});
        // img-chooser
        
		final Animation anim_rotate = AnimationUtils.loadAnimation(Old_Vote.this, R.anim.rotate);
		
    	vote_left.setOnClickListener(new OnClickListener()
    		{
			@Override
			public void onClick(View v)
				{
				// Resource-ID aus Array auslesen
				if(vote_id-1 >= 0)
					{
					vote_id -= 1;
					vote_img.setImageDrawable(vote_image.getDrawable(vote_id));
					spinner.setSelection(vote_id);
					}
				else
					{
					vote_left.startAnimation(anim_rotate);
			        }
				}
    		});
    	
    	vote_right.setOnClickListener(new OnClickListener()
			{
			@Override
			public void onClick(View v)
				{
				// Resource-ID aus Array auslesen
				if(vote_id+1 < vote_image.length())
					{
					vote_id += 1;
					vote_img.setImageDrawable(vote_image.getDrawable(vote_id));
					spinner.setSelection(vote_id);
					}
				else
					{
					vote_right.startAnimation(anim_rotate);
					}
				}
			});
    	
    	((Button)findViewById(R.id.btn_vote_submit)).setOnClickListener(new OnClickListener()
			{
			@Override
			public void onClick(View v)
				{
				SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(Old_Vote.this);
				SharedPreferences.Editor pref_edit = pref.edit();
				pref_edit.putBoolean("vote_online", true);
				pref_edit.putInt("vote_id", vote_id);
				pref_edit.commit();
				new vote_online().execute();
				setResult(vote_id);
				finish();
				}
			});
        }
    
    public class vote_online extends AsyncTask<Void, Integer, Boolean>
		{
    	SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(Old_Vote.this);
		
    	@Override
		protected void onPostExecute(Boolean result)
			{
			}
	
		@Override
		protected Boolean doInBackground(Void... params)
			{
			HttpClient client = new DefaultHttpClient();
	        HttpPost post = new HttpPost(URI.create((getResources().getString(R.string.url_vote_send))));	// Ziel des HTTP Requests
	        try {
	            ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
	            nameValuePair.add(new BasicNameValuePair("vote_id", vote_image.getString(pref.getInt("vote_id", 0))));
	            nameValuePair.add(new BasicNameValuePair("action", "0"));
	            // Debug information
	            nameValuePair.add(new BasicNameValuePair("device_country", Locale.getDefault().getDisplayCountry()));
	            nameValuePair.add(new BasicNameValuePair("device_language", Locale.getDefault().getLanguage()));
	            nameValuePair.add(new BasicNameValuePair("device_model", android.os.Build.MODEL));
	            nameValuePair.add(new BasicNameValuePair("device_manufacturer", android.os.Build.MANUFACTURER));
	            // Debug information
	            post.setEntity(new UrlEncodedFormEntity(nameValuePair, "UTF-8"));
	            HttpResponse response = client.execute(post);
	            StatusLine statusLine = response.getStatusLine();
	            if(statusLine.getStatusCode() == HttpURLConnection.HTTP_OK)
	            	{
	                return true;
	            	}
	        	}
	        catch (Exception e) {}
	        return false;
			}
		}

  
	@Override
	public void onPause()
		{
		super.onPause();
		}
	
	@Override
	public void onResume()
		{
		super.onResume();
		}
	
	@Override
	public void onDestroy()
		{
		super.onDestroy();
		}
		*/
    	}
	}